package com.xcc.annotation.ioc.config;

import com.xcc.annotation.ioc.bean.Dog;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class Demo02_MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * AnnotationMetadata：当前类的注解信息
     * BeanDefinitionRegistry:BeanDefinition注册类；
     * 		把所有需要添加到容器中的bean；调用
     * 		BeanDefinitionRegistry.registerBeanDefinition手工注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
                                        BeanDefinitionRegistry registry) {
        boolean dogDao = registry.containsBeanDefinition("com.xcc.annotation.ioc.bussines.MyDogDao");
        boolean dogService = registry.containsBeanDefinition("com.xcc.annotation.ioc.bussines.MyDogService");
        //如果有dogDao和dogService 就注入dog
        if (dogDao && dogService) {
            RootBeanDefinition beanDefinition = new RootBeanDefinition(Dog.class);
            //向容器中注入当前类
            registry.registerBeanDefinition("dog",beanDefinition);
        }
    }
}
